package cn.edu.ecjtu.android_tutorials;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class UserEditDialog {

    AlertDialog.Builder builder;
    AlertDialog dialog;
    Button btnCancel;
    Button btnEdit;
    EditText name;
    EditText phone;
    public interface Callback {
        void onEditName(String name,String phone);
    }
    public Callback callback;

    public UserEditDialog(Activity activity) {
        builder = new AlertDialog.Builder(activity);
        View v = activity.getLayoutInflater().inflate(R.layout.dialog_edit_user, null);
        builder.setView(v);
        dialog = builder.create();
        dialog.setCancelable(true);
    }

    public void show() {
        dialog.show();

        name = dialog.findViewById(R.id.name);
        phone = dialog.findViewById(R.id.phone);

        btnCancel  = dialog.findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        btnEdit = dialog.findViewById(R.id.btn_edit);
        btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (callback != null) {
                    callback.onEditName(name.getText().toString(),phone.getText().toString());
                }
                dialog.dismiss();
            }
        });

    }
}
